<template>
  <div>
    <!-- <h2>学生列表</h2> -->
	<brand></brand>
  <div id="container" v-show="isShow">
    <div id="out">
      <div id="title"><span id="student_name">姓名: {{showStudent.name}}</span><span id="close" @click="isShow = false">×</span></div>
      <div  id="lookUp">
        <div id="img">
      <img :src="showStudent.image" alt="" v-if="showStudent.image">
      <span v-else>无</span>
    </div>
    <div>
      <p>编号：{{showStudent._id}}</p>
      <p>性别：{{showStudent.gender}}</p>
      <p>年龄：{{showStudent.age}}</p>
      <p v-if="showStudent.subjectsId">专业：{{showStudent.subjectsId.name}}</p>
      <p v-else>专业：无</p>
      <p v-if="showStudent.classesId">班级：{{showStudent.classesId.name}}</p>
      <p v-else>班级：无</p>
    </div>
      </div>
  </div>
  </div>
    <div id="box">
          <select name="" id="" v-model="choose">
              <option value="name">姓名</option>
              <option value="age">年龄</option>
              <option value="gender">性别</option>
          </select>
          <input type="text" class="input" v-model="input">
          <button @click="searchStudents()">确认搜索</button>
        </div>
    <table border="1">
      <thead>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>专业</th>
        <th>班级</th>
        <th>头像</th>
        <th>操作</th>
      </thead>
      <tbody>
        <tr v-for="student in students" :key="student._id">
              <td>{{student._id}}</td>
              <td>{{student.name}}</td>
              <td>{{student.age}}</td>
              <td>{{student.gender}}</td>
          <td v-if=student.subjectsId>{{student.subjectsId.name}}</td>
          <td v-else>无</td>
          <td v-if=student.classesId>{{student.classesId.name}}</td>
          <td v-else>无</td>
          <td v-if=student.image><img :src='student.image' alt=""></td>
          <td v-else>无</td>
          <td><a class="lookup" href="#" @click.prevent="lookUp(student)">查看</a> <a class="update" href="#" @click.prevent="getupdateStudent(student._id)">修改</a> <a class="del" href="#" @click.prevent="delStudent(student._id,student.name,student.age,student.gender)">删除</a></td>
        </tr>
      </tbody>
    </table>
    <div id="page">
      <span>共 {{total}} 条</span>
      <select name="" id="" v-model="pageSize">
        <option value="10">10条/每页</option>
        <option value="5">5条/每页</option>
        <option value="15">15条/每页</option>
        <option value="20">20条/每页</option>
      </select>
      <button class="target" @click="currentPage=1">首页</button>
      <button @click="prePage()" :class='{disabled:currentPage==1}'>&lt;</button>
      <span>{{currentPage}}</span>
      <button @click="nextPage()" :class='{disabled:currentPage==totalPages}'>&gt;</button>
      <button class="target" @click="currentPage=totalPages">尾页</button>
      <span>{{currentPage}} / {{totalPages}}</span>
      <span>前往</span>
      <input type="text" value="1" @blur="toPage()" v-model="goPage">
      <span>页</span>
      </div>
  </div>
</template>

<script>

import brand from '../common/brand.vue'

export default {
	components:{
		brand
	},
  data() {
    return {
      isShow:false,
      showStudent:{},
      students:[],
      choose:'name',
      input:'',
      pageSize:10,
      currentPage:1,
      total:0,
      totalPages:0,
      goPage:1,
      flag:false
    }
  },
  watch:{
    pageSize:{
        handler () {
          // 当侦听的数据发生变化时要执行的操作...
        this.currentPage = 1;
        if(!this.flag){
          this.getStudents();
        }else{
          this.searchStudents()
        }
            },
        deep: true // 深度侦听
    },
    currentPage:{
        handler () {
          // 当侦听的数据发生变化时要执行的操作...
        if(!this.flag){
          this.getStudents();
        }else{
          this.searchStudents()
        }
            },
        deep: true // 深度侦听
    }
  },
created(){
        this.getStudents();
    },

  methods:{
    //获取学生
    async getStudents(){

        //封装后
        const {data} = await this.$api.students.getStudents(this.pageSize,this.currentPage);

        this.students = data.data.rows;
        this.total = data.data.total;
        this.totalPages = data.data.pages;
        console.log(data.data);
    },

    prePage(){
      if(this.currentPage>1){
        this.currentPage--;
        if(!this.flag){
          this.getStudents();
        }else{
          this.searchStudents()
        }
      }
    },
    nextPage(){
      if(this.currentPage<this.totalPages){
        this.currentPage++;
        if(!this.flag){
          this.getStudents();
        }else{
          this.searchStudents()
        }
      }
    },
    toPage(){
		if(this.goPage<=this.totalPages&&this.goPage>=1){
			this.currentPage = this.goPage;
			if(!this.flag){
          this.getStudents();
        }else{
          this.searchStudents()
        }
		}
    },


    lookUp(student){
      this.isShow = true;
      this.showStudent = student;
      console.log(student);
    },

    //获取要修改的学生
        async getupdateStudent(id){
        console.log(id);
        this.$router.push('/home/updateStudent/' + id);
        },
    
    //删除学生
    async delStudent(id,name,age,sex){

      const {data} = await this.$api.students.delStudent(id,name,age,sex);

      let result = data;
      console.log(result);
      if(data.code){
        alert(data.message);
      }else{
        alert(data.message);
      }
      //删除后再获取学生
      this.getStudents();
    },

    //搜索学生
      async searchStudents(){
      this.flag = true;
      console.log(this.choose,this.input);
      const {data} = await this.$api.students.searchStudents(this.choose,this.input,this.pageSize,this.currentPage);
      this.total = data.data.total;
      this.totalPages = data.data.pages;
      if(this.currentPage > this.totalPages){
        this.currentPage = 1;
      }
      this.students = data.data.rows;
    },
  }
}
</script>

<style scoped lang='scss'>
#container{
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba($color: #000000, $alpha: 0.3);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  // display: none;
}
#out{
  background-color: #fff;
  padding: 20px 30px;
}
#student_name{
  font-size: 20px;
  font-weight: bold;
}
#close{
  position: absolute;
  right: -15px;
  top: -15px;
  font-size: 34px;
  cursor: pointer;
  transition: all 0.5s;
  &:hover{
    color: royalblue;
    transform: rotate(360deg);
  }
}
#title{
  position: relative;
  display: flex;
  justify-content: space-between;
}
#lookUp{
  display: flex;
  position: relative;
  // width: 480px;
  // height: 300px;
  border-radius: 2px;
  color: #000;
  z-index: 100;
  display: flex;
  align-items: center;
  // justify-content: space-between;
}
#img{
  width: 150px;
  height: 150px;
  margin-right: 40px;
  border: 1px dashed #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  img{
    width: 100%;
    height: 100%;
    border-radius: 0;
  }
}
h2{
  margin: 0;
  margin-bottom: 10px;
}
#box{
  text-align: center;
  margin-bottom: 20px;
select{
    width: 70px;
    height: 36px;
    padding-left: 2px;
    outline: none;
    margin-right: 13px;
    border: 1px solid #dedede;
    border-radius: 4px;
}
.input{
    width: 260px;
    height: 32px;
    padding-left: 8px;
    outline: none;
    margin-right: 20px;
    border: 1px solid #dedede;
    border-radius: 4px;
}
input:focus{
        border: 1px solid #2f54eb;
        box-shadow:  0px 0px 0 1px #BDE7FF, 0px 0px 0 1px #BDE7FF;
        }
button{
    width: 80px;
    height: 35px;
    border: 1px solid #e1e1e1;
    border-radius: 4px;
    outline: none;
    cursor: pointer;
}
button:hover{
    background-color: rgb(73, 73, 245);
    color: white;
}
}
table{
  width: 100%;
  border:1px solid #ebeef5;
  border-collapse: collapse;
  tr{
    cursor: pointer;
    &:hover{
      background-color: #f5f7fa;
    }
  }
  th,td{
    height: 50px;
    text-align: center;
    // color: #909399;
    &:last-child{
      width: 300px;
    }
    &:first-child{
      width: 350px;
    }
  }
}
img{
  width: 40px;
  height: 40px;
  border-radius: 20px;
}
a{
  display: inline-block;
  text-decoration: none;
  width: 56px;
  height: 30px;
  line-height: 30px;
  margin: 0 4px;
  border-radius: 3px;
  font-size: 14px;
}
.lookup{
  background-color: #2f54eb;
  color:#fff;
  &:hover{
    background-color: #5976ef;
  }
}
.update{
  border: 1px solid #dcdfe6;
  &:hover{
    background-color: #eaeefd;
  }
}
.del{
  background-color: #f56c6c;
  color: #fff;
  &:hover{
    background-color: #f78989;
  }
}
#page{
  margin-top: 15px;
  span{
    margin: 0 8px;
    font-size: 15px;
  }
  select{
    width: 100px;
    height: 30px;
    text-align: center;
  }
  button{
    width: 30px;
    height: 30px;
    margin: 5px;
  }
  .target{
    width: 50px;
  }
  .disabled{
    cursor: not-allowed;
    &:hover{
      background-color: #efefef;
      color: #000;
    }
  }
  input{
    width: 45px;
    height: 28px;
    text-align: center;
    outline: none;
    padding: 0;
    border: 1px solid #dcdfe6;
    border-radius: 2px;
  }
}

</style>